<?php
/**
 * Created by JetBrains PhpStorm.
 * User: valera
 * Date: 8/14/11
 * Time: 5:22 PM
 * To change this template use File | Settings | File Templates.
 */
 
class ProductController extends ZendX_Controller_Action
{
    protected $_resources = array( 'mysql' );

    public function indexAction() {
        $this->_loadResource('layout');
    }

    public function searchAction() {
        $this->_helper->getHelper('viewRenderer')->setNoRender(true);
        $this->_loadResources(array('sphinx', 'mysql'));
        $page = $this->getRequest()->getParam('page', 1);
        $ipp = $this->getRequest()->getParam('ipp', 10);
        $filters = $this->getRequest()->getParam('filters', array());
        $sorts = (array) $this->getRequest()->getParam('sort', array( 'type' => 'name', 'order' => 'asc' ));
        $search = $this->getRequest()->getParam('search', NULL);
        $cache = false;
        $easy_way = false;
        $field = 'name';
        if($search === NULL || empty($search)) {
            $field = 'empty';
            $search = '1';
            $cache = true;
            if(empty($filters)) {
                $easy_way = true;
            }
        } else {
            $search = "*$search*";
        }

        $request = ZendX_Sphinx::Query('products', $field)
                ->filter($filters)
                ->search($search);

        if($sorts['type'] === 'name') {
            $sorts['type'] = $sorts['type'] . '_atr';
        }
        $request->sortedBy($sorts['type'], $sorts['order'] == 'asc');
        if($sorts['type'] === 'name_atr') {
            $sorts['type'] = 'name';
        }

        if($easy_way) {
            $request->offset(($page - 1) * $ipp, $ipp);
        }

        $res = NULL;
        $res = ZendX_Sphinx::getInstance()->request($request);
        $res->useCache($cache, $request->getSign(), $easy_way);
        $db = Zend_Registry::get('Zend_Db');

        $mc = new ZendX_Cache_Manager('products');
        $result = $mc->getItems($res->getIds($page, $ipp));
        $filters_init = array();
        $map = array(
            'top_id' => 'categories',
            'category_id' => 'categories',
            'brand_id' => 'brands'
        );
        $start_time = microtime(true);
        foreach(array('top_id', 'category_id', 'brand_id') as $type) {
            $cm = new ZendX_Cache_Manager($map[$type]);
            if(array_key_exists($type, $filters)) {
                $item = $cm->getItems($filters[$type]);
                $filters_init[$type]['id'] = $item['id'];
                $filters_init[$type]['title'] = $item['title'];
                $filters_init[$type]['type'] = 'value';
            } else {
                $items = $cm->getItems($res->getElements($type,true));
                $filters_init[$type]['type'] = 'list';
                $filters_init[$type]['items'] = array();
                foreach($items as $item) {
                    $new_elem = array();
                    $new_elem['id'] = $item['id'];
                    $new_elem['title'] = $item['title'];
                    $filters_init[$type]['items'][] = $new_elem;
                }
            }
        }
        $end_time = microtime(true);

        $data = array(
            'items' => array(),
            'total' => $res->getTotal(),
            'page' => $page,
            'search' => $search,
            'sort' => $sorts
        );
        $data = array_merge_recursive($data, $filters_init);
        foreach($result as $item) {
            $data['items'][]  = array( 'id' => $item['id'], 'title' => $item['name'], 'price' => sprintf('%.02f', (float)$item['price']) );
        }
        $data['time'] = sprintf('%.04f', $end_time - $start_time);
        print json_encode($data);
    }
}
